<?PHP
include("settings.php");
include("connect.php");
include("functions.php");
limits();
include("queries.php");
include ("graph/jpgraph.php");
include ("graph/jpgraph_line.php");
include ("graph/jpgraph_bar.php");

$prices = array();
$ticks = array();
$quantity = array();

if ($_GET['graph'] == 'balance' && isset($_GET['wallet'])) {
$query = "SELECT `balance`,`date` FROM `".$settings[$_GET['wallet'].'journaltable']."`".($_GET['wallet'] == 'corp' ? " WHERE `division` = '1000'" : "")." ORDER BY `date` DESC LIMIT 2000";
$result = $db->query($query);
while ($row = $db->fetch_assoc($result)) {
$datetime = explode(" ", $row['date']);
array_push($ticks, $datetime[0]);
array_push($prices, $row['balance']);
}
} # end balance

if ($_GET['graph'] == 'week') {
$result = $db->query($query16);
while ($row = $db->fetch_assoc($result)) {
$datetime = explode(" ", $row['transactionDateTime']);
$prices[$datetime[0]] = ($prices[$datetime[0]] + ($row[price]*$row[quantity]));
$quantity[$datetime[0]] = ($quantity[$datetime[0]] + $row[quantity]);
}
foreach ($prices as $date => $amount)
{
$average[$date] = number_format(($amount / $quantity[$date]),2,'.','');
}
$prices = array_values($average);
$quantity = array_values($quantity);
$ticks = array_keys($average);
$prices = array_reverse($prices);
$ticks = array_reverse($ticks);
$quantity = array_reverse($quantity);
} # end week

elseif ($_GET['graph'] == 'month') {
$result = $db->query($query17);
while ($row = $db->fetch_assoc($result)) {
$datetime = explode(" ", $row['transactionDateTime']);
$prices[$datetime[0]] = ($prices[$datetime[0]] + ($row[price]*$row[quantity]));
$quantity[$datetime[0]] = ($quantity[$datetime[0]] + $row[quantity]);
}
foreach ($prices as $date => $amount)
{
$average[$date] = number_format(($amount / $quantity[$date]),2,'.','');
}
$prices = array_values($average);
$quantity = array_values($quantity);
$ticks = array_keys($average);
$prices = array_reverse($prices);
$ticks = array_reverse($ticks);
$quantity = array_reverse($quantity);
} # end month

else {
$result = $db->query($query10);
while ($row = $db->fetch_assoc($result)) {
array_push($prices, $row['price']);
$datetime = explode(" ", $row['transactionDateTime']);
array_push($ticks, $datetime[0]);
array_push($quantity, $row['quantity']);
}
$prices = array_reverse($prices);
$ticks = array_reverse($ticks);
$quantity = array_reverse($quantity);
} # end else by order


$graph = new Graph(600,400,"auto");

$ydata = $prices;

if (isset($quantity) && !empty($quantity)) {
$y2data = $quantity;
$graph->SetY2Scale("int");
$lineplot2=new BarPlot($y2data);
$graph->AddY2($lineplot2);
$graph->y2axis->SetColor("orange");
$graph->y2axis->SetLabelFormatCallback('yLabelFormat');
//$graph->y2axis->title->Set("Quantity");
$graph->y2axis->title->SetFont(FF_FONT1,FS_BOLD);
$lineplot2->SetLegend('Quantity');
$lineplot2->SetColor("orange");
$lineplot2->SetWeight(1);
$lineplot2->SetFillColor("orange");
}


if ($_GET['graph'] == 'balance') {
$graph->img->SetMargin(95,10,20,80);
} else {
$graph->img->SetMargin(95,70,20,80);
}
$graph->SetMarginColor('white');
$graph->SetFrame(false);
$graph->SetScale("textlin");



$lineplot=new LinePlot($ydata);

$graph->Add($lineplot);
$graph->yaxis->SetColor("blue");
$graph->yaxis->SetLabelFormatCallback('yLabelFormat');

$graph->ygrid->SetFill(true,'#EFEFEF@0.5','#BBCCFF@0.5');

$graph->xaxis->SetTickLabels($ticks);
$graph->xaxis->SetColor("#BBCCFF");
if ($_GET['graph'] == 'balance') $graph->xaxis->SetTextLabelInterval(199);
//$graph->xaxis->SetTextLabelInterval(2);
$graph->xaxis->SetLabelAngle(90);

//$graph->xaxis->title->Set("Transaction Date");
//$graph->yaxis->title->Set("Price");

$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

$lineplot->SetBarCenter();
if ($_GET['graph'] == 'balance') {
$lineplot->SetLegend('Balance');
} else {
$lineplot->SetLegend('Price');
}
$lineplot->SetColor("blue");
$lineplot->SetWeight(1);

$graph->legend->SetFillColor('#BBCCFF@0.5',5);
$graph->legend->SetShadow('gray@0.4',5);
$graph->legend->SetPos(0,0,'right','top');
// Display the graph
$graph->Stroke();
?>